package com.hzw.saas.service.project.flow.service;

import java.util.Objects;
import javax.annotation.Resource;

import com.hzw.saas.api.user.IUserAccountService;
import com.hzw.saas.api.user.bo.UserAccountBO;
import com.hzw.saas.common.flow.exception.FlowServiceHandleException;
import com.hzw.saas.common.flow.pojo.model.FlowInstance;
import com.hzw.saas.common.flow.pojo.model.FlowProcessRecord;
import com.hzw.saas.common.flow.service.FlowActionHandle;
import com.hzw.saas.common.verify.service.IEmailService;
import com.hzw.saas.service.project.constants.ProjectConsts;
import com.hzw.saas.service.project.service.IProjectApplyService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 08/13/2021
 */
@Service(ProjectConsts.PROJECT_APPLY_SERVICE_RETURN)
@Slf4j
public class ProjectApplyServiceReturnImpl implements FlowActionHandle {

    @Resource
    private IProjectApplyService projectApplyService;
    @Resource
    private IEmailService emailService;
    @Resource
    private IUserAccountService userAccountService;

    @Override
    public void handle(FlowInstance flowInstance, FlowProcessRecord flowProcessRecord) throws FlowServiceHandleException {
        UserAccountBO account = userAccountService.getAccount(flowInstance.getCreatorId());
        if (Objects.isNull(account) || StrUtil.isBlank(account.getEmail())) {
            log.error("回退邮件提醒失败：用户为空或用户邮箱为空: {}", flowInstance.getCreatorId());
            return;
        }
        emailService.send(account.getEmail(), flowInstance.getFlowInstanceName(), "您的申请已被退回", false);
    }

}
